package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.RackEquipment;
import com.ruoyi.business.mapper.RackEquipmentMapper;
import com.ruoyi.business.service.IRackEquipmentService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 台架设备Service业务层处理
 * 
 * @author qiancheng
 * @date 2025-06-13
 */
@Service
public class RackEquipmentServiceImpl extends ServiceImpl<RackEquipmentMapper, RackEquipment> implements IRackEquipmentService
{
    @Autowired
    private RackEquipmentMapper rackEquipmentMapper;

    @Value("${business.equipment.host}")
    private String host;

    @Value("${server.port}")
    private int port;

    @Value("${business.equipment.preview-path}")
    private String previewPath;

    /**
     * 查询台架设备
     * 
     * @param id 台架设备主键
     * @return 台架设备
     */
    @Override
    public RackEquipment selectRackEquipmentById(Long id)
    {
        return rackEquipmentMapper.selectRackEquipmentById(id);
    }

    /**
     * 查询台架设备
     *
     * @param id 台架设备主键
     * @return 台架设备
     */
    @Override
    public RackEquipment selectRackEquById(Long id)
    {
        return rackEquipmentMapper.selectRackEquById(id);
    }

    /**
     * 实验平台管理->>台架维护管理->>查询台架设备列表
     * 
     * @param rackEquipment 台架设备
     * @return 台架设备
     */
    @Override
    public List<RackEquipment> selectRackEquipmentList(RackEquipment rackEquipment)
    {
        List<RackEquipment> rackEquipments = rackEquipmentMapper.selectRackEquipmentList(rackEquipment);

        if (rackEquipments != null && !rackEquipments.isEmpty()) {
            for (RackEquipment equipment : rackEquipments) {
                String draw = equipment.getDraw();
                if (draw != null && !draw.trim().isEmpty()) {
                    String[] files = draw.split(",");
                    List<String> urls = new ArrayList<>();
                    for (String file : files) {
                        String url = "http://" + host + ":" + port + previewPath + file.trim();
                        urls.add(url);
                    }
                    equipment.setDraws(urls);
                } else {
                    equipment.setDraws(Collections.emptyList());
                }
            }
        }
        return rackEquipments;
    }

    /**
     * 实验平台管理->>台架维护管理->>新增台架设备
     * 
     * @param rackEquipment 台架设备
     * @return 结果
     */
    @Override
    public String insertRackEquipment(MultipartFile file,RackEquipment rackEquipment) throws IOException {
        rackEquipment.setCreateTime(DateUtils.getNowDate());
        rackEquipment.setDraw(rackEquipment.getDraw());
        rackEquipment.setUseTime(rackEquipment.getPurTime());
        rackEquipmentMapper.insert(rackEquipment);
        return rackEquipment.getDraw();
    }

    /**
     * 实验平台管理->>台架维护管理->>修改台架设备
     * 
     * @param rackEquipment 台架设备
     * @return 结果
     */
    @Override
    public int updateRackEquipment(RackEquipment rackEquipment)
    {
        rackEquipment.setUpdateTime(DateUtils.getNowDate());
        return rackEquipmentMapper.updateRackEquipment(rackEquipment);
    }

    /**
     * 实验平台管理->>台架维护管理->>批量删除台架设备
     * 
     * @param ids 需要删除的台架设备主键
     * @return 结果
     */
    @Override
    public int deleteRackEquipmentByIds(Long[] ids)
    {
        return rackEquipmentMapper.deleteRackEquipmentByIds(ids);
    }

    /**
     * 删除台架设备信息
     * 
     * @param id 台架设备主键
     * @return 结果
     */
    @Override
    public int deleteRackEquipmentById(Long id)
    {
        return rackEquipmentMapper.deleteRackEquipmentById(id);
    }

    /**
     * 根据台架ID查询设备列表
     * 
     * @param rackId 台架ID
     * @return 台架设备集合
     */
    @Override
    public List<RackEquipment> selectRackEquipmentByRackId(Long rackId)
    {
        return rackEquipmentMapper.selectRackEquipmentByRackId(rackId);
    }

    /**
     * 根据台架ID删除设备
     * 
     * @param rackId 台架ID
     * @return 结果
     */
    @Override
    public int deleteRackEquipmentByRackId(Long rackId)
    {
        return rackEquipmentMapper.deleteRackEquipmentByRackId(rackId);
    }
}
